package com.hongxun.design.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.hongxun.design.R;
import com.hongxun.design.activitys.OfflineMapActivity;
import com.hongxun.design.adapters.DownloadmanagementAdapter;

import java.util.ArrayList;

/**
 * Created by zerdoor_pc on 2016/7/13.
 */
public class DownloadmanagementFragment extends BaseFragment {
    OfflineMapActivity offlineMapActivity = null;
    ListView downladlayout_list;
    ArrayList<MKOLUpdateElement> loadData;
    DownloadmanagementAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View newsLayout = inflater.inflate(R.layout.fragment_downloadlayout, container,
                false);
        offlineMapActivity = (OfflineMapActivity) getActivity();
        findView(newsLayout);
        return newsLayout;
    }

    @Override
    void findView(View v) {
        super.findView(v);
        downladlayout_list = (ListView) v.findViewById(R.id.downladlayout_list);
        loadData = new ArrayList<>();
        setAdapter();
        downladlayout_list.setAdapter(adapter);
    }


    private void addData() {
        if (offlineMapActivity.localMapList == null) {
            loadData.clear();
            return;
        }
        if (loadData == null) {
            loadData = offlineMapActivity.localMapList;
        } else {
            loadData.clear();
            loadData.addAll(offlineMapActivity.localMapList);
        }

    }

    public void setAdapter() {
        addData();
        if (loadData.size() == 0) {
            MKOLUpdateElement element = new MKOLUpdateElement();
            element.cityName = "ok";
            loadData.add(element);
        }
        if (adapter == null) {
            adapter = new DownloadmanagementAdapter(offlineMapActivity, loadData);
            downladlayout_list.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }
}
